package com.secneo.appshare.threads;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.secneo.appshare.api.LocalStore;
import com.secneo.appshare.api.impl.RemoteApiImpl;
import com.secneo.appshare.common.Constants;

public class RemindCountThread extends Thread{

	Context ctx = null ;
	Handler handler = null ;
	
	public RemindCountThread(Context ctx , Handler handler){
		this.ctx = ctx ;
		this.handler = handler ;
	}
	
	public void run(){
		if(LocalStore.getLocaleStoreInstance(ctx).getUid() != null &&
				!("".equals(LocalStore.getLocaleStoreInstance(ctx).getUid()))){
			RemoteApiImpl apiImpl = new RemoteApiImpl() ;
			RemoteApiImpl.News news = new RemoteApiImpl.News() ;
			Log.d("TAG" , "RemindCountThread run") ;
			if(apiImpl.getNews(LocalStore.getLocaleStoreInstance(ctx).getUid()
					, news) == 1){
				if(news.newActivityCount != 0)
					LocalStore.getLocaleStoreInstance(ctx).
						setNoteCounts(news.newActivityCount) ;
				if(news.newFanCount != 0)
					LocalStore.getLocaleStoreInstance(ctx).
							setFollowerCounts(news.newFanCount) ;
				if(news.newSmsCount != 0)
					LocalStore.getLocaleStoreInstance(ctx).
							setSMSCounts(news.newSmsCount) ;
				if(news.newHonorCount != 0)
					LocalStore.getLocaleStoreInstance(ctx).
							setHonorCounts(news.newHonorCount) ;
				
				if(news.newSmsCount != 0 || news.newActivityCount != 0 || 
						news.newFanCount != 0||news.newHonorCount != 0){
					
					Intent intent = new Intent(Constants.REMIND_ACTION);
					intent.putExtra(Constants.NEW_FANS_COUNT_TAG, news.newFanCount) ;
					intent.putExtra(Constants.NEW_Activity_COUNT_TAG, news.newActivityCount) ;
					intent.putExtra(Constants.NEW_MSG_COUNT_TAG, news.newSmsCount) ;
					intent.putExtra(Constants.NEW_HONOR_COUNT_TAG, news.newHonorCount) ;
					ctx.sendBroadcast(intent);
					
					Message msg1 = new Message() ;
					msg1.what = 2 ;
					Bundle data = new Bundle() ;
					data.putInt(Constants.NEW_FANS_COUNT_TAG, news.newFanCount) ;
					data.putInt(Constants.NEW_Activity_COUNT_TAG, news.newActivityCount) ;
					data.putInt(Constants.NEW_MSG_COUNT_TAG, news.newSmsCount) ;
					data.putInt(Constants.NEW_HONOR_COUNT_TAG, news.newHonorCount) ;
					msg1.setData(data) ;
					handler.sendMessage(msg1) ;
				}
			} else {
				Message msg = new Message() ;
				msg.what = 3 ;
				handler.sendMessage(msg) ;
			}
		}
	}
	
}
